<!--
  -   设备管理.   :permission="permissionList"
  - Author: lengleng (wangiegie@gmail.com)
  -->
<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        :page="page"
        :data="tableData"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load="getList"
        @search-change="searchChange"
        @refresh-change="refreshChange"
        @size-change="sizeChange"
        @current-change="currentChange"
        @row-del="handleDelete"
      >
        <template
          slot="menuLeft"
          slot-scope="scope"
        >
          <el-button
            v-if="estate_rmproject_add"
            class="filter-item"
            type="primary"
            icon="el-icon-plus"
            @click="addOrUpdateHandle('add','')"
          >
            新增
          </el-button>
        </template>

        <template
          slot="projectNumber"
          slot-scope="scope"
        >
          <el-link
            type="primary"
            :underline="false"
            @click="handleView('view',scope.row.id)"
          >{{scope.row.projectNumber}}</el-link>
        </template>
        <template
          slot="modifyContractId"
          slot-scope="scope"
        >
          <el-link
            type="primary"
            :underline="false"
            @click="handleContractView('view',scope.row.ctContract.id)"
          >{{scope.row.ctContract==null?'':scope.row.ctContract.contractNumber}}</el-link>
        </template>

        <template
          slot="menu"
          slot-scope="scope"
        >
          <el-button
            v-if="estate_rmproject_get"
            type="text"
            @click="handleView('view',scope.row.id)"
          >
            查看
          </el-button>
          <el-button
            v-if="estate_rmproject_edit"
            type="text"
            @click="addOrUpdateHandle('edit',scope.row.id)"
          >
            编辑
          </el-button>
          <el-button
            v-if="estate_rmproject_del"
            type="text"
            @click="handleDelete(scope.row,scope.index)"
          >
            删除
          </el-button>
        </template>
      </avue-crud>

      <table-form
        v-if="addOrUpdateVisible"
        ref="addOrUpdate"
        @refreshDataList="getList"
      ></table-form>
      <table-forms
        v-if="viewVisible"
        ref="viewBtn"
        @refreshDataList="getList"
      ></table-forms>
      <CtContractModifys
        v-if="viewConVisible"
        ref="viewContract"
      ></CtContractModifys>

    </basic-container>
  </div>
</template>

<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.14.1/xlsx.full.min.js"></script>
<script>
import { fetchList, getObj, addObj, putObj, delObj } from '@/api/market/device/person'
import { tableOption } from '@/const/crud/market/device/person'
import { mapGetters } from 'vuex'

export default {
  name: 'device',
  data () {
    return {
      addOrUpdateVisible: false,
      viewVisible: false,
      viewConVisible: false,
      tableLoading: true,
      searchForm: {},
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      uploadlg: {
        show: false,
        noUploadFile: true
      },
      proBuildTypeList: [],
      proStatusList: [],
      projectPropertyList: [],
      tableOption: tableOption,
      estate_rmproject_get: false,
      estate_rmproject_add: false,
      estate_rmproject_edit: false,
      estate_rmproject_del: false,
      estate_rmproject_import: false,
      estate_rmproject_export: false
    }
  },
  created () {
    this.estate_rmproject_get = this.permissions['estate_rmproject_get']
    this.estate_rmproject_add = this.permissions['estate_rmproject_add']
    this.estate_rmproject_edit = this.permissions['estate_rmproject_edit']
    this.estate_rmproject_del = this.permissions['estate_rmproject_del']
    this.estate_rmproject_export = this.permissions['estate_rmproject_export']
    this.estate_rmproject_import = this.permissions['estate_rmproject_import']

    //房产状态
    //getProjectStatus().then(response => {
    //  if (response.data != null) {
    //    this.proStatusList = response.data.data;
    //  }
    //})

    //房产性质
    //getProjectpProperty().then(response => {
    //  if (response.data != null) {
    //    this.projectPropertyList = response.data.data;
    //  }
    //})

    //获取房产类型
    //getProjectType().then(response => {
    //  if (response.data != null) {
    //    this.proBuildTypeList = response.data.data;
    //  }
    //})

  },
  computed: {
    ...mapGetters(['elements', 'permissions'])
  },
  methods: {
    getList (page, params) {
      //this.tableData = [{ "id": "14", "projectNumber": "B1-17-19", "buildingType": "10", "projectFloorNumber": "1", "createTime": "2018-04-20 07:15:18", "updateTime": "2020-07-14 19:04:15", "projectUnitId": "10", "projectName": "20", "projectArea": "170", "projectStatus": "100", "modifyContractId": "Z20190917001", "projectProperty": "持有" }]
      //this.page.total = 1
      this.tableLoading = true
      fetchList(Object.assign({
        current: this.page.currentPage,
        size: this.page.pageSize
      }, params, this.searchForm)).then(response => {
        this.tableData = response.data.data.records;
        this.page.total = response.data.data.total;
        this.tableLoading = false;
      }).catch(() => {
        this.tableLoading = false
      })
    },
    // 删除
    handleDelete: function (row, index) {
      if (row.projectStatus == 1 || row.projectStatus == 3) {
        this.$message.warning('房产已租/已售,不允许删除！')
        return;
      }
      this.$confirm('是否确认删除此条数据？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delObj(row.id)
      }).then(data => {
        this.$message.success('删除成功！')
        this.getList(this.page)
      })
    },
    //合同查看
    handleContractView: function (method, id) {
      this.viewConVisible = true
      this.$nextTick(() => {
        this.$refs.viewContract.init(method, id)
      })
    },
    // 查看
    handleView: function (method, id) {
      this.viewVisible = true
      this.$nextTick(() => {
        this.$refs.viewBtn.init(method, id)
      })
    },
    // 新增、修改
    addOrUpdateHandle (isEdit, id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(isEdit, id)
      })
    },

    sizeChange (pageSize) {
      this.page.pageSize = pageSize
    },
    currentChange (current) {
      this.page.currentPage = current
    },
    searchChange (form, done) {
      this.searchForm = form
      this.page.currentPage = 1
      this.getList(this.page, form)
      done()
    },
    refreshChange () {
      this.getList(this.page)
    }
  }
}
</script>
